package com.syqc.monitor.ui.repair

import androidx.activity.viewModels
import androidx.recyclerview.widget.GridLayoutManager
import autodispose2.autoDispose
import com.luck.picture.lib.entity.LocalMedia
import com.syqc.adapter.ChoosePhotoAdapter
import com.syqc.choose.car.ChooseCarActivity
import com.syqc.choose.teamcar.TeamsCarsActivity
import com.syqc.comlib.base.BaseBarActivity
import com.syqc.comlib.rx.doAfterChanged
import com.syqc.comlib.rx.filterFast
import com.syqc.comlib.utils.ActivityResultUtil.launchAct
import com.syqc.comlib.utils.ActivityResultUtil.registerResultOk
import com.syqc.comlib.utils.ToolbarHelper
import com.syqc.monitor.R
import com.syqc.monitor.databinding.ActivityUpRepairBinding
import com.syqc.monitor.databinding.IncludeRecyclerWrapBinding
import com.syqc.monitor.databinding.IncludeSelectSureBinding
import com.syqc.monitor.ui.newaddcar.PhotoImagePopup
import com.syqc.utils.InputUtil
import com.syqc.utils.Keys
import com.syqc.utils.NetDataUtil


/**
 *@Author Jasper
 *@Time   2021/1/14
 *@Desc   维修申报
 */
class SubmitRepairActivity :
    BaseBarActivity<ActivityUpRepairBinding>(ActivityUpRepairBinding::inflate) {
    private lateinit var mSureVB: IncludeSelectSureBinding
    private lateinit var mRecyclerVB: IncludeRecyclerWrapBinding
    private val mPhotoPopup by lazy { PhotoImagePopup(this, 1, this::callback) }
    private val mAdapter by lazy { ChoosePhotoAdapter() }
    private val mViewModel by viewModels<SubmitRepairViewModel>()

    override fun initView() {
        mSureVB = IncludeSelectSureBinding.bind(binding.root)
        mRecyclerVB = IncludeRecyclerWrapBinding.bind(binding.root)
        ToolbarHelper.setToolbar(this, R.string.title_other_request)
        mSureVB.tvSure.setText(R.string.desc_com_submit)
        InputUtil.setInputChat(binding.editSubmitRepairName, 10, false)
        InputUtil.setInputRemark(binding.editSubmitRepairDesc, 50)
        mRecyclerVB.recyclerWrap.layoutManager = GridLayoutManager(this, 3)
        mRecyclerVB.recyclerWrap.adapter = mAdapter

        mViewModel.handlerPhoto(null)
        mViewModel.toastObserver().autoDispose(scopeProvider).subscribe(this::toastObserver)

        mViewModel.photosObserver().autoDispose(scopeProvider).subscribe {
            mAdapter.setList(it)
            mAdapter.notifyDataSetChanged()
        }

        initIntent()
    }

    override fun setListener() {

        binding.lrvChooseCar.filterFast {

            if (NetDataUtil.isSyncTree) {
                launcher.launchAct<TeamsCarsActivity>(this)
            } else {
                launcher.launchAct<ChooseCarActivity>(this)
            }
        }

        binding.editSubmitRepairName.doAfterChanged {
            mViewModel.repairName = it
        }

        binding.editSubmitRepairPhone.doAfterChanged {
            mViewModel.repairPhone = it
        }

        binding.editSubmitRepairDesc.doAfterChanged {
            mViewModel.repairDesc = it
        }

        mAdapter.setOnItemClickListener { adapter, view, position ->
            val path = mAdapter.data[position]
            if (path.isEmpty()) {
                mPhotoPopup.showAtLocation(mViewModel.nowNum)
            }
        }

        mAdapter.setOnItemChildClickListener { adapter, view, position ->
            mViewModel.removePhoto(position)
        }

        mSureVB.tvSure.filterFast {
            mViewModel.submitRepair()
        }
    }

    private val launcher = registerResultOk { result ->
        result.data?.let {
            val carId = it.getStringExtra(Keys.INTENT_CAR_ID) ?: ""
            val carName = it.getStringExtra(Keys.INTENT_CAR_NAME) ?: ""
            binding.lrvChooseCar.setRightText(carName)
            mViewModel.carId = carId
        }
    }

    private fun callback(type: Int, result: ArrayList<LocalMedia>?) {
        mViewModel.handlerPhoto(result)
    }

    private fun initIntent() {
        val carId = intent.getStringExtra(Keys.INTENT_CAR_ID)
        val carName = intent.getStringExtra(Keys.INTENT_PLATE) ?: ""
        if (carId == null) return
        mViewModel.carId = carId
        binding.lrvChooseCar.setRightText(carName)
        binding.lrvChooseCar.isEnabled = false
    }
}